package com.iqik.base.alg;

import java.io.IOException;
import java.util.*;

public class BaseDemo2 {
    class Inner {
        public String  v1 = "Fake News";
        public String v2 = "Go ahead";
    }

    private static String GetVal() {
        try {
            int a = 1 / 0;
            return Inner.class.newInstance().v1;
        } catch (Exception e) {
            try {
                return Inner.class.getDeclaredConstructor(BaseDemo2.class).newInstance((BaseDemo2)null).v2;
            } catch (Exception ee) {
                ee.printStackTrace();
                return "Fake News, Go ahead";
            }
        }
    }

    private static Integer method(Integer i){
        try{
            if(i++ > 0)
                throw new IOException();
            return i++;
        }catch (IOException e){
            i++;
            return i++;
        }catch (Exception e){
            i++;
            return i++;
        }finally {
            return i++;
        }
    }

    public static int calc(int n) {
        try {
            n += 1;
            if(n/0 > 0) {
                n+=1;
            } else {
                n-=10;
            }
            return n;
        } catch (Exception e) {
            n++;
        }
        n ++;
        return n;
    }

    static void cnn() {
        System.out.print("CNN ");
    }

    public static Set<String> showMap(Map<String, Object> map) {
        if (map == null || map.size() ==0)
            return null;
        Set<String> sets = new HashSet<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String target = null;
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof String) {
                target = String.format("%s.%s", key, value);
            }
            if (value instanceof ArrayList) {

            }
            if (value instanceof Integer) {
                target = String.format("%s.%d", key, value);
            }
            if (value instanceof Map) {
                Set<String> strings = showMap((HashMap)value);
                strings.forEach((str) -> {
                    sets.add(str);
                });
            }
            sets.add(target);
        }

        return sets;
    }

    public static void main(String[] args) {

//        int[][] nums = {{0, 1, 2}, {3, 4, 5}};
//        System.out.println(nums.length);
        Map<String, Object> map = new HashMap<>();
        map.put("k1", 1);
        map.put("k2", "v2");
        Set<String> strings = showMap(map);
        strings.forEach(str -> {
            System.out.println(str);
        });

//        System.out.print(method(2));
//        System.out.println(calc(10));

        /*Thread t = new Thread() {
            public void run() {
                cnn();
            }
        };
        t.run();
        System.out.print("FakeNews ");
        System.out.print("; ");
        t.start();
        System.out.print("FakeNews ");*/

//        System.out.println(GetVal());
        /*Map<String, Object> map = new HashMap<>();
        String str = "没人比我更懂java";
        StrObject obj = new StrObject("没人比我更懂java");
        map.put("str", str);
        map.put("obj", obj);

        str = "真的没人比我更懂java";
        System.out.printf(map.get("str").toString()+"; ");

        StrObject new_obj = (StrObject) map.get("obj");
        new_obj.setStr("真的没人比我更懂java");
        System.out.printf(map.get("obj").toString()+"; ");*/
    }

    static class StrObject{
        String str;
        public StrObject(String str){
            this.str = str;
        }
        public void setStr(String str){
            this.str = str;
        }
        @Override
        public String toString() {
            return str;
        }
    }
}
